﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace Viewer.BusinessObject.BusinessEntities
{
    public class ChucNang : BusinessBase
    {
        public List<QUModule> LoadAllChucNang()
        {
            return this.Context.QUModules.ToList();
        }
        public IEnumerable LoadDataRootTree()
        {
            return this.Context.QUModules.Where(a => a.ModuleLevel.Equals(1) && a.ModuleIDCha.Equals(0)).ToList();
        }
        public List<QUModule> LoadDataNode(int IdCha)
        {
            return this.Context.QUModules.Where(a => a.ModuleIDCha.Equals(IdCha)).ToList();
        }
        public IEnumerable LoadDataForGrid(int Id)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var query = from tc in db.QUModules
                        where tc.ModuleIDCha.Equals(Id)
                        select new
                        {
                            Id = tc.ModuleID,
                            TenChucNang = tc.TenModuleFull,
                            Tencha = LayTenCha(tc.ModuleIDCha),
                            GhiChu = tc.GhiChu
                        };
            return query;
        }
        private string LayTenCha(int IdCha)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var query = (from d in db.QUModules
                         where d.ModuleID.Equals(IdCha)
                         select d).Distinct();
            if (query != null && query.Count() > 0)
            {
                return query.SingleOrDefault().TenModuleFull;
            } return string.Empty;


        }
        public QUModule GetChucNangById(int id)
        {
            return this.Context.QUModules.Where(a => a.ModuleID.Equals(id)).Single();
        }
        public List<string> LoadChucNangById(int Id)
        {
            List<string> lst = new List<string>();
            DataClassesDataContext db = new DataClassesDataContext();
            var query = from tc in db.QUModules
                        where tc.ModuleID.Equals(Id)
                        select new
                        {
                            Id = tc.ModuleID,
                            TenModuleShort = tc.TenModuleShort,
                            TenModuleFull = tc.TenModuleFull,
                            IdCha = tc.ModuleIDCha,
                            GhiChu = tc.GhiChu,
                            TenCha = LayTenCha(tc.ModuleIDCha),
                            Link = tc.Link,
                            Icon = tc.IconName
                        };
            foreach (var item in query)
            {
                lst.Add(item.Id.ToString());
                lst.Add(item.TenModuleFull);
                lst.Add(item.TenModuleShort);
                lst.Add(item.IdCha.ToString());
                lst.Add(item.TenCha.ToString());
                lst.Add(item.GhiChu);
                lst.Add(item.Link);
                lst.Add(item.Icon);
            }
            return lst;
        }
        public void InsertChucNang(string tenShort, string tenFull, int idToChucCha, int toChucLevel, string ghiChu, string link, string icon)
        {
            using (var context = new DataClassesDataContext())
            {
                QUModule item = new QUModule();
                item.TenModuleShort = tenShort;
                item.TenModuleFull = tenFull;
                item.ModuleIDCha = idToChucCha;
                item.ModuleLevel = idToChucCha > 0 ? 2 : 1;
                item.GhiChu = ghiChu;
                item.IconName = icon;
                item.Link = link;
                context.QUModules.InsertOnSubmit(item);
                context.SubmitChanges();
            }
        }
        public void UpdateChucNang(int id, string shortName, string full, string ghichu,string link,string icon)
        {
            using (var context = new DataClassesDataContext())
            {
                var item = context.QUModules.Single(a => a.ModuleID.Equals(id));
                item.TenModuleShort = shortName;
                item.TenModuleFull = full;
                item.GhiChu = ghichu;
                item.Link = link;
                item.IconName = icon;
                context.SubmitChanges();
            }
        }
        public IEnumerable SearchByCondition(int nodeId, string ten, int toChucCha)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var query = from tc in db.QUModules
                        where (nodeId > 0 ? tc.ModuleIDCha.Equals(nodeId) : true)
                        select new
                        {
                            Id = tc.ModuleID,
                            TenChucNang = tc.TenModuleFull,
                            Tencha = LayTenCha(tc.ModuleIDCha),
                            GhiChu = tc.GhiChu,
                            ModuleIDCha = tc.ModuleIDCha
                        };
            if (!string.IsNullOrEmpty(ten))
            {
                query = query.Where(a => a.TenChucNang.ToLower().Contains(ten.ToLower()));
            }
            if (toChucCha>0)
            {
                query = query.Where(a => a.ModuleIDCha.Equals(toChucCha));
            }
            return query;
        }
        public void DeleteChucNang(int id)
        {
            using (var context = new DataClassesDataContext())
            {
                var item = context.QUModules.Single(a => a.ModuleID.Equals(id));
                context.QUModules.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
        }
    }
}
